import axios from 'axios'
import config from './config'
export default class tool {
    constructor() {}

    // 将对象中的属性值置为'' 或 0
    clearOb(ob) {
        Object.keys(ob).forEach((key) => {
            if (typeof ob[key] == "number") {
                ob[key] = 0
            } else {
                ob[key] = ''
            }
        })
    }

    // 部分接口需要设置请求头'Content-type': "application/json"
    httpForJson() {
        let http = axios.create({
            baseURL: config(),
            headers: {
                'Content-type': "application/json"
            }
        })
        http.interceptors.response.use(response => {
            const {
                data
            } = response
            if (data && data != "") {
                return data
            } else {
                return "";
            }
        }, error => {

            return Promise.reject(error)
        })
        return http
    }
    // 时间戳转标准时间格式 ：yyyy-MM-dd HH:mm:ss
    timestampToString(timestamp) {
        let date = new Date(timestamp)
        let timeString = date.toLocaleDateString().split('/')
            .map(e => {
                if (e.length < 2) e = e.padStart(2, 0)
                return e
            })
            .join('-')
        timeString +=
            ` ${date.getHours().toString().padStart(2, 0)}:${date.getMinutes().toString().padStart(2, 0)}:${date.getSeconds().toString().padStart(2, 0)}`
        return timeString
    }
}